package br.com.gip.business.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import br.com.gip.business.CategoriaBusiness;
import br.com.gip.exception.BusinessException;
import br.com.gip.repository.dao.CategoriaDao;
import br.com.gip.repository.model.Categoria;

@Service
public class CategoriaBusinessImpl implements CategoriaBusiness {

	private static final long serialVersionUID = -5544152010206316942L;

	@Autowired
	private CategoriaDao categoriaDao;

	@Override
	public List<Categoria> buscarCategorias() {
		return this.categoriaDao.findAll();
	}
	
	@Override
	public List<Categoria> buscarCategoria(String nome) {
		return this.categoriaDao.findByFilter(nome);
	}

	@Override
	public void salvarCategoria(Categoria categoria) {
		if (categoria.getCategoriaId() == null) {
			boolean categoriaValida = this.buscarCategoria(categoria.getNome()).isEmpty();
			if (!categoriaValida) {
				throw new BusinessException("Categoria ja existente");
			}
		}
		
		this.categoriaDao.saveOrUpdate(categoria);
	}
	
}
 